package com.didang;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;



import com.didang.exception.NoPkAnnotationException;
import com.didang.exception.PkValueIsNullException;

/**
 * model基类，定义各种数据库访问方法
 * @author didang
 *
 */
public abstract class Model {

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private SqlVO wheresVO;
	
	public int update() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, NoPkAnnotationException, PkValueIsNullException{
	    SqlVOGenerator sqlVOgene = new SqlVOGenerator(this);
	    SqlVO sqlVO = sqlVOgene.generateUpdateSqlVO();
	    return jdbcTemplate.update(sqlVO.getSql(), sqlVO.getParams());
	}

	public int insert() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, NoPkAnnotationException, PkValueIsNullException{
	    SqlVOGenerator sqlVOgene = new SqlVOGenerator(this);
	    SqlVO sqlVO = sqlVOgene.generateInsertSqlVO();
	    return jdbcTemplate.update(sqlVO.getSql(), sqlVO.getParams());
	}
	
	public int delete() throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, NoPkAnnotationException, PkValueIsNullException{
	    SqlVOGenerator sqlVOgene = new SqlVOGenerator(this);
	    SqlVO sqlVO = sqlVOgene.generateDeleteSqlVO();
	    return jdbcTemplate.update(sqlVO.getSql(), sqlVO.getParams());
	}
	
	public SqlVO getWheresVO() {
		return wheresVO;
	}

	public void setWheresVO(SqlVO wheresVO) {
		this.wheresVO = wheresVO;
	}
}
